home *** CD-ROM | disk | FTP | other *** search
/ Scene 96 / Scene 96 International Edition (Zyklop Software) (Disc 2) (1997).iso / misc / coding / midas060 / src / rawf_nt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-16  |  12.4 KB  |  447 lines

  1. /*      rawf_nt.c
  2.  *
  3.  * Win32 raw file I/O for MIDAS Sound System
  4.  *
  5.  * $Id: rawf_nt.c,v 1.3 1997/01/16 18:41:59 pekangas Exp $
  6.  *
  7.  * Copyright 1996,1997 Housemarque Inc.
  8.  *
  9.  * This file is part of the MIDAS Sound System, and may only be
  10.  * used, modified and distributed under the terms of the MIDAS
  11.  * Sound System license, LICENSE.TXT. By continuing to use,
  12.  * modify or distribute this file you indicate that you have
  13.  * read the license and understand and accept it fully.
  14. */
  15.  
  16. #define WIN32_LEAN_AND_MEAN
  17. #include <windows.h>
  18.  
  19. #include "lang.h"
  20. #include "mtypes.h"
  21. #include "errors.h"
  22. #include "mmem.h"
  23. #include "rawfile.h"
  24.  
  25. RCSID(const char *rawf_nt_rcsid = "$Id: rawf_nt.c,v 1.3 1997/01/16 18:41:59 pekangas Exp $";)
  26.  
  27.  
  28. /****************************************************************************\
  29. *
  30. * Function:     int ErrorCode(void)
  31. *
  32. * Description:  Get the MIDAS error code corresponding to GetLastError()
  33. *
  34. * Returns:      MIDAS error code
  35. *
  36. \****************************************************************************/
  37.  
  38. static int ErrorCode(void)
  39. {
  40.     #define MAPERR(win32, midas) case win32: return midas;
  41.  
  42.     switch ( GetLastError() )
  43.     {
  44.             MAPERR( NO_ERROR,               OK )
  45.             MAPERR( ERROR_ACCESS_DENIED,    errAccessDenied )
  46.             MAPERR( ERROR_ALREADY_EXISTS,   errFileExists )
  47.             MAPERR( ERROR_BAD_NET_NAME,     errInvalidPath )
  48.             MAPERR( ERROR_BAD_NETPATH,      errInvalidPath )
  49.             MAPERR( ERROR_CANNOT_MAKE,      errFileWrite )
  50.             MAPERR( ERROR_DEV_NOT_EXIST,    errInvalidPath )
  51.             MAPERR( ERROR_DIRECTORY,        errInvalidPath )
  52.             MAPERR( ERROR_DISK_FULL,        errDiskFull )
  53.             MAPERR( ERROR_FILE_EXISTS,      errFileExists )
  54.             MAPERR( ERROR_FILE_NOT_FOUND,   errFileNotFound )
  55.             MAPERR( ERROR_HANDLE_DISK_FULL, errDiskFull )
  56.             MAPERR( ERROR_INVALID_COMPUTERNAME, errInvalidPath )
  57.             MAPERR( ERROR_INVALID_DRIVE,    errInvalidPath )
  58.             MAPERR( ERROR_INVALID_HANDLE,   errInvalidFileHandle )
  59.             MAPERR( ERROR_INVALID_NAME,     errInvalidPath )
  60.             MAPERR( ERROR_INVALID_SHARENAME, errInvalidPath )
  61.             MAPERR( ERROR_NOT_ENOUGH_MEMORY, errOutOfMemory )
  62.             MAPERR( ERROR_OPEN_FAILED,      errFileOpen )
  63.             MAPERR( ERROR_OUTOFMEMORY,      errOutOfMemory )
  64.             MAPERR( ERROR_READ_FAULT,       errFileRead )
  65.             MAPERR( ERROR_SEEK,             errFileRead )
  66.             MAPERR( ERROR_TOO_MANY_OPEN_FILES, errTooManyFiles )
  67.             MAPERR( ERROR_WRITE_FAULT,      errFileWrite )
  68.         default:
  69.             return errUndefined;
  70.     }
  71. }
  72.  
  73.  
  74.  
  75.  
  76. /****************************************************************************\
  77. *
  78. * Function:     int rfOpen(char *fileName, int openMode, rfHandle *file);
  79. *
  80. * Description:  Opens a file for reading or writing
  81. *
  82. * Input:        char *fileName          name of file
  83. *               int openMode            file opening mode, see enum rfOpenMode
  84. *               rfHandle *file          pointer to file handle
  85. *
  86. * Returns:      MIDAS error code.
  87. *               File handle is stored in *file.
  88. *
  89. \****************************************************************************/
  90.  
  91. int CALLING rfOpen(char *fileName, int openMode, rfHandle *file)
  92. {
  93.     int         error;
  94.     rfHandle    hdl;
  95.     HANDLE      handle;
  96.  
  97.     /* allocate file structure */
  98.     if ( (error = memAlloc(sizeof(rfFile), (void**) &hdl)) != OK )
  99.         PASSERROR(ID_rfOpen)
  100.  
  101.     switch ( openMode )
  102.     {
  103.         case rfOpenRead:        /* open file for reading */
  104.             handle = CreateFile(fileName, GENERIC_READ, FILE_SHARE_READ,
  105.                 NULL, OPEN_EXISTING, 0, NULL);
  106.             break;
  107.  
  108.         case rfOpenWrite:       /* open file for writing */
  109.             handle = CreateFile(fileName, GENERIC_WRITE, 0,
  110.                 NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  111.             break;
  112.  
  113.         case rfOpenReadWrite:   /* open file for reading and writing */
  114.             handle = CreateFile(fileName, GENERIC_READ | GENERIC_WRITE, 0,
  115.                 NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  116.             break;
  117.     }
  118.  
  119.     /* If an error occurred during opening file, return the error code
  120.        specified by GetLastError(): */
  121.     if ( handle == INVALID_HANDLE_VALUE )
  122.     {
  123.         error = ErrorCode();
  124.  
  125.         if ( error == OK )
  126.             error = errFileNotFound;
  127.  
  128.         ERROR(error, ID_rfOpen);
  129.         return error;
  130.     }
  131.  
  132.     /* Store handle: */
  133.     hdl->f = (U32) handle;
  134.  
  135.     /* store file handle in *file: */
  136.     *file = hdl;
  137.  
  138.     return OK;
  139. }
  140.  
  141.  
  142.  
  143.  
  144. /****************************************************************************\
  145. *
  146. * Function:     int rfClose(rfHandle file);
  147. *
  148. * Description:  Closes a file opened with rfOpen().
  149. *
  150. * Input:        rfHandle file           handle of an open file
  151. *
  152. * Returns:      MIDAS error code
  153. *
  154. \****************************************************************************/
  155.  
  156. int CALLING rfClose(rfHandle file)
  157. {
  158.     int         error;
  159.     HANDLE      handle = (HANDLE) file->f;
  160.  
  161.     /* close file: */
  162.     if ( !CloseHandle(handle) )
  163.     {
  164.         /* error occurred - return error code from GetLastError: */
  165.         error = ErrorCode();
  166.         ERROR(error, ID_rfClose);
  167.         return error;
  168.     }
  169.  
  170.     /* deallocate file structure: */
  171.     if ( (error = memFree(file)) != OK )
  172.         PASSERROR(ID_rfClose)
  173.  
  174.     return OK;
  175. }
  176.  
  177.  
  178.  
  179.  
  180. /****************************************************************************\
  181. *
  182. * Function:     int rfGetSize(rfHandle file, long *fileSize);
  183. *
  184. * Description:  Get the size of a file
  185. *
  186. * Input:        rfHandle file           handle of an open file
  187. *               ulong *fileSize         pointer to file size
  188. *
  189. * Returns:      MIDAS error code.
  190. *               File size is stored in *fileSize.
  191. *
  192. \****************************************************************************/
  193.  
  194. int CALLING rfGetSize(rfHandle file, long *fileSize)
  195. {
  196.     int         error;
  197.     static long fpos;
  198.  
  199.     /* store current file position: */
  200.     if ( (error = rfGetPosition(file, &fpos)) != OK )
  201.         PASSERROR(ID_rfGetSize)
  202.  
  203.     /* seek to end of file: */
  204.     if ( (error = rfSeek(file, 0, rfSeekEnd)) != OK )
  205.         PASSERROR(ID_rfGetSize)
  206.  
  207.     /* read file position to *filesize: */
  208.     if ( (error = rfGetPosition(file, fileSize)) != OK )
  209.         PASSERROR(ID_rfGetSize)
  210.  
  211.     /* return original file position: */
  212.     if ( (error = rfSeek(file, fpos, rfSeekAbsolute)) != OK )
  213.         PASSERROR(ID_rfGetSize)
  214.  
  215.     return OK;
  216. }
  217.  
  218.  
  219.  
  220.  
  221. /****************************************************************************\
  222. *
  223. * Function:     int rfRead(rfHandle file, void *buffer, ulong numBytes);
  224. *
  225. * Description:  Reads binary data from a file
  226. *
  227. * Input:        rfHandle file           file handle
  228. *               void *buffer            reading buffer
  229. *               ulong numBytes          number of bytes to read
  230. *
  231. * Returns:      MIDAS error code.
  232. *               Read data is stored in *buffer, which must be large enough
  233. *               for it.
  234. *
  235. \****************************************************************************/
  236.  
  237. int CALLING rfRead(rfHandle file, void *buffer, ulong numBytes)
  238. {
  239.     int         error;
  240.     HANDLE      handle = (HANDLE) file->f;
  241.     DWORD       numRead;
  242.  
  243.     if ( !ReadFile(handle, buffer, numBytes, &numRead, NULL) )
  244.     {
  245.         /* Return error code from GetLastError: */
  246.         error = ErrorCode();
  247.         ERROR(error, ID_rfRead);
  248.         return error;
  249.     }
  250.  
  251.     /* Check that we did read all data: */
  252.     if ( numRead != numBytes )
  253.     {
  254.         ERROR(errEndOfFile, ID_rfRead);
  255.         return errEndOfFile;
  256.     }
  257.  
  258.     return OK;
  259. }
  260.  
  261.  
  262.  
  263.  
  264. /****************************************************************************\
  265. *
  266. * Function:     int rfWrite(rfHandle file, void *buffer, ulong numBytes);
  267. *
  268. * Description:  Writes binary data to a file
  269. *
  270. * Input:        rfHandle file           file handle
  271. *               void *buffer            pointer to data to be written
  272. *               ulong numBytes          number of bytes to write
  273. *
  274. * Returns:      MIDAS error code
  275. *
  276. \****************************************************************************/
  277.  
  278. int CALLING rfWrite(rfHandle file, void *buffer, ulong numBytes)
  279. {
  280.     int         error;
  281.     HANDLE      handle = (HANDLE) file->f;
  282.     DWORD       numWritten;
  283.  
  284.     if ( !WriteFile(handle, buffer, numBytes, &numWritten, NULL) )
  285.     {
  286.         /* Return error code from GetLastError: */
  287.         error = ErrorCode();
  288.         ERROR(error, ID_rfWrite);
  289.         return error;
  290.     }
  291.  
  292.     /* Check that we did write all data: */
  293.     if ( numWritten != numBytes )
  294.     {
  295.         ERROR(errFileWrite, ID_rfWrite);
  296.         return errFileWrite;
  297.     }
  298.  
  299.     return OK;
  300. }
  301.  
  302.  
  303.  
  304.  
  305. /****************************************************************************\
  306. *
  307. * Function:     int rfSeek(rfHandle file, long newPosition, int seekMode);
  308. *
  309. * Description:  Seeks to a new position in file. Subsequent reads and writes
  310. *               go to the new position.
  311. *
  312. * Input:        rfHandle file           file handle
  313. *               long newPosition        new file position
  314. *               int seekMode            file seek mode, see enum rfSeekMode
  315. *
  316. * Returns:      MIDAS error code
  317. *
  318. \****************************************************************************/
  319.  
  320. int CALLING rfSeek(rfHandle file, long newPosition, int seekMode)
  321. {
  322.     HANDLE      handle = (HANDLE) file->f;
  323.     int         error;
  324.     DWORD       moveMethod;
  325.     LONG        moveDist = newPosition;
  326.  
  327.     /* Select pointer move starting point: */
  328.     switch ( seekMode )
  329.     {
  330.         case rfSeekAbsolute:            /* seek to an absolute offset */
  331.             moveMethod = FILE_BEGIN;
  332.             break;
  333.  
  334.         case rfSeekRelative:            /* seek relative to current */
  335.             moveMethod = FILE_CURRENT;
  336.             break;
  337.  
  338.         case rfSeekEnd:                 /* seek from end of file */
  339.             moveMethod = FILE_END;
  340.             break;
  341.  
  342.         default:
  343.             /* invalid seek mode: */
  344.             ERROR(errInvalidArguments, ID_rfSeek);
  345.             return errInvalidArguments;
  346.     }
  347.  
  348.     /* Seek to new position: */
  349.     if ( SetFilePointer(handle, moveDist, NULL, moveMethod) == 0xFFFFFFFF )
  350.     {
  351.         /* Unable to seek - check the error from GetLastError(): */
  352.         error = ErrorCode();
  353.         ERROR(error, ID_rfSeek);
  354.         return error;
  355.     }
  356.  
  357.     return OK;
  358. }
  359.  
  360.  
  361.  
  362.  
  363. /****************************************************************************\
  364. *
  365. * Function:     int rfGetPosition(rfHandle file, long *position);
  366. *
  367. * Description:  Reads the current position in a file
  368. *
  369. * Input:        rfHandle file           file handle
  370. *               long *position          pointer to file position
  371. *
  372. * Returns:      MIDAS error code.
  373. *               Current file position is stored in *position.
  374. *
  375. \****************************************************************************/
  376.  
  377. int CALLING rfGetPosition(rfHandle file, long *position)
  378. {
  379.     HANDLE      handle = (HANDLE) file->f;
  380.     int         error;
  381.     DWORD       pos;
  382.  
  383.     /* Get position by using SetFilePointer(): */
  384.     if ( (pos = SetFilePointer(handle, 0, NULL, FILE_CURRENT)) == 0xFFFFFFFF )
  385.     {
  386.         /* Error - check GetLastError(): */
  387.         error = ErrorCode();
  388.         ERROR(error, ID_rfGetPosition);
  389.         return error;
  390.     }
  391.  
  392.     *position = pos;                    /* store position in *position */
  393.  
  394.     return OK;
  395. }
  396.  
  397.  
  398.  
  399.  
  400. /****************************************************************************\
  401. *
  402. * Function:     int rfFileExists(char *fileName, int *exists);
  403. *
  404. * Description:  Checks if a file exists or not
  405. *
  406. * Input:        char *fileName          file name, ASCIIZ
  407. *               int *exists             pointer to file exists status
  408. *
  409. * Returns:      MIDAS error code.
  410. *               *exists contains 1 if file exists, 0 if not.
  411. *
  412. \****************************************************************************/
  413.  
  414. int CALLING rfFileExists(char *fileName, int *exists)
  415. {
  416.     HANDLE      handle;
  417.  
  418.     /* Attempt to open the file: */
  419.     handle = CreateFile(fileName, GENERIC_READ, FILE_SHARE_READ, NULL,
  420.         OPEN_EXISTING, 0, NULL);
  421.  
  422.     if ( handle == INVALID_HANDLE_VALUE )
  423.     {
  424.         /* Couldn't be opened: return 0: */
  425.         *exists = 0;
  426.         return OK;
  427.     }
  428.  
  429.     /* Close the file: */
  430.     CloseHandle(handle);
  431.  
  432.     return OK;
  433. }
  434.  
  435.  
  436. /*
  437.  * $Log: rawf_nt.c,v $
  438.  * Revision 1.3  1997/01/16 18:41:59  pekangas
  439.  * Changed copyright messages to Housemarque
  440.  *
  441.  * Revision 1.2  1996/09/22 23:16:33  pekangas
  442.  * Fixed to always return an error from rfOpenFile if CreateFile fails
  443.  *
  444.  * Revision 1.1  1996/08/13 20:49:10  pekangas
  445.  * Initial revision
  446.  *
  447. */